package com.more.offer.threadtest.forkjoin;

import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;

/**
 * @author yinpeng10
 * @desc
 */
public class CountTask extends RecursiveTask<Integer> {

	public static final int taskNum = 10;

	private int start;
	private int end;

	public CountTask() {
	}

	public CountTask(int start, int end) {
		this.start = start;
		this.end = end;
	}

	@Override
	protected Integer compute() {
		int sumResult = 0;
		if((end - start) < taskNum) {
			for (int i = start; i <=end; i++) {
				sumResult+=i;
			}
		} else {
			System.out.println("子任务继续拆分： start " + start +" end " + end);
			int middle = (end + start) / 2;
			CountTask left = new CountTask(start, middle);
			CountTask right = new CountTask(middle+1, end);
			left.fork();
			right.fork();
			sumResult = left.join() + right.join();
		}
		return sumResult;
	}
}
